import sim.util.*;

/*
 * This class represents any agent on the grid.  Each agent is either a Civilian or Cop. 
 */

public abstract class Agent{
	Int2D loc;
	boolean active = false;
	boolean grouped = false;
	int jailTerm = 0;
	
	IntBag neighborsX = new IntBag();
	IntBag neighborsY = new IntBag();
	
	public Agent(Int2D loc){
		this.loc = loc;
	}
	
	protected void move(EpsteinGrid epState){		
		Bag freeSpaces = new Bag(); // bag of free spaces
		
		//Loop through all agents within vision
		for(int i = 0; i < neighborsX.size(); i++){			
			int x = neighborsX.get(i);
			int y = neighborsY.get(i);	
			
			Bag agents = epState.grid.getObjectsAtLocation(x, y);
			
			//if the space is empty, add it to the bag of free spaces
			if(agents == null){
				freeSpaces.add(new Int2D(x, y));
			}
			//if the space is occupied by agents that are in jail, add it to the bag of free spaces
			else if(agents != null){
				boolean freeSpace = false;
				
				Agent agent;
				for(int j = 0; j < agents.size(); j++){
					agent = (Agent)agents.get(j);
				
					if(agent.jailTerm > 0){
						freeSpace = true;		
					}
					else{
						freeSpace = false;
						break;
					}
				}
				
				if(freeSpace == true){
					freeSpaces.add(new Int2D(x, y));
				}
			}
		}
		
		//select random location from free spaces bag and move there
		if(freeSpaces.size() > 0){
			loc = (Int2D) freeSpaces.get(epState.random.nextInt(freeSpaces.size()));
			epState.grid.setObjectLocation(this, loc);
		}
	}
}
